//
//  RenameViewController.m
//  
//
//  Created by MTiMac02 on 2017/11/8.
//
//

#import "RenameViewController.h"
#import "MyAltasViewController.h"
#import "IGUpdateAtlasApi.h"

@interface RenameViewController ()
@property (strong, nonatomic) IBOutlet UITextField *renameText;
@property(nonatomic,strong)IGUpdateAtlasApi *updateTitleApi;

@end

@implementation RenameViewController
#pragma mark -生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title=@"重命名";
     self.title=NSLocalizedString(@"Rename", nil);
//     [self ig_setupRightButtonItemWithTitle:@"确定" image:nil highLightedImage:nil];
    [self ig_setupRightButtonItemWithTitle:NSLocalizedString(@"Confirm", nil) image:nil highLightedImage:nil];
     [self setUI];
}
-(void)setUI{
    [_renameText setPlaceholder:[NSString stringWithFormat:@"%@",self.name]];
    _renameText.leftViewMode = UITextFieldViewModeAlways;
    _renameText.rightViewMode = UITextFieldViewModeUnlessEditing;
    _renameText.font = [UIFont font16];
    _renameText.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, 0)];
    _renameText.textColor=[UIColor c4Color];
    _renameText.clearButtonMode = UITextFieldViewModeWhileEditing;
    [_renameText addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
}
//导航栏右侧按钮点击事件
- (void)ig_rightButtonPressed:(id)sender {
    if (_renameText.text.length==0) {
        _renameText.text=[NSString stringWithFormat:@"%@",self.name];
    }
    BOOL ishas = [NSString stringContainsEmoji:_renameText.text];
    NSLog(@" %@ 表情", ishas?@"有":@"没有");
    if (ishas)
    {
//        [self presentMessageTips:@"不支持表情符号"];
        [self presentMessageTips:NSLocalizedString(@"Emoji Unavailable", nil)];
        return;
    }
    self.updateTitleApi=[[IGUpdateAtlasApi alloc]initWithUid:[IGUser getUid] atlasId:self.albumId title:_renameText.text];
   
    [self.updateTitleApi startWithDecodeClass:[IGUpdateAtlasResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error)
     {
         if (responseObject) {
             [self.navigationController popViewControllerAnimated:YES];
         }
     }];
}
-(void)pushVC{
    
    [self.navigationController popViewControllerAnimated:YES];
}
//编辑图册名称
- (void)textChange:(UITextField *)textView {
    NSString *toBeString=textView.text;
    UITextRange *selectedRang=[textView markedTextRange];
    UITextPosition *position=[textView positionFromPosition:selectedRang.start offset:0];
    if (!position) {
        NSStringEncoding enc=CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000 );
        NSData *da=[toBeString dataUsingEncoding:enc];
        NSInteger dataLength=[da length];
        if(dataLength>20){
            int lll=(int)[toBeString length];
            for (int z=0; z<lll; z++) {
                NSString *fromString=[toBeString substringToIndex:z];
                NSStringEncoding enc2=CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
                NSData *da2FROM=[fromString dataUsingEncoding:enc2];
                NSInteger fromDataLength2=[da2FROM length];
                if (fromDataLength2==20) {
                    textView.text=fromString;
                }
                if (fromDataLength2>20) {
//                    [self presentMessageTips:@"图册名称过长"];
                    [self presentMessageTips:NSLocalizedString(@"Album Name Overlong", nil)];
                    return;
                }
            }
        }
        else{
            textView.text=toBeString;
        }
    }
    _renameText=textView;
    
}
-(IBAction)textFiledReturnEditing:(id)sender{
    [sender resignFirstResponder];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_renameText resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
